package com.zl_huang.example.web;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.MessageSource;
import org.springframework.context.i18n.LocaleContextHolder;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.LocaleResolver;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;


import com.zl_huang.example.model.Subject;
import com.zl_huang.example.service.ISubjectService;
import com.zl_huang.example.service.IUserService;


@Controller
public class SubjectController {
Map<Integer ,Subject> mapSubject = new HashMap<Integer ,Subject>();
	
	@Autowired
	private ISubjectService subjectService ;
	@Autowired
	private LocaleResolver localeResolver;
	@Autowired
	private IUserService  userService;

	
	@Autowired
	private MessageSource messageSource;
	
	@RequestMapping("/subject")
	public String subject(Model model){
		List<Subject> list;
		try {
			list = subjectService.findAllSubject();
			
			if(!list.isEmpty()){
				for(Subject subject : list){
					mapSubject.put(subject.getSub_id(), subject);
				}
			}
			model.addAttribute("edit", messageSource.getMessage("page.index.edit", null,LocaleContextHolder.getLocale()));
			model.addAttribute("subjectList", list);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "subject/SubjectList";
	}
	
	@RequestMapping(value="/addSubject" , method = RequestMethod.GET)
	public String toAdd(Model model){
		model.addAttribute("subject", new Subject());
		return "subject/add";
	}
	
	@RequestMapping(value="/addSubject" , method = RequestMethod.POST)
	public String add(RedirectAttributes redirectAttributes , @Validated Subject subject, BindingResult result){
		
		try {		
			    subjectService.createSubject(subject);
				redirectAttributes.addFlashAttribute("message", "添加成功");
				return "redirect:/subject";	
		} catch (Exception e) {
			e.printStackTrace();
			return "error";
		}
		
	}
	
	@RequestMapping(value = "/editSubject/{sub_id}" , method = RequestMethod.GET)
	public String toEdit(Model model , @PathVariable int sub_id ){
		Subject subject;
		try {
			subject = subjectService.getSubjectById(sub_id);
			model.addAttribute("subject" , subject );
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return "subject/edit";
	}
	
	@RequestMapping(value="/editSubject/{sub_id}" , method=RequestMethod.POST)
	public String edit(RedirectAttributes redirectAttributes , Subject subject , @PathVariable int sub_id){
		
		
		try {
			subject.setSub_id(sub_id);
			subjectService.editSubject(subject);
			redirectAttributes.addFlashAttribute("message", "修改成功");
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "redirect:/subject";		
	}
	@RequestMapping(value="/deleteSubject/{sub_id}" , method=RequestMethod.GET)
	public String delete(RedirectAttributes redirectAttributes , @PathVariable int sub_id){
		try {
			subjectService.deleteSubject(sub_id);
			redirectAttributes.addFlashAttribute("message", "删除成功");
		} catch (Exception e) {
			redirectAttributes.addFlashAttribute("message", "删除失败");
			e.printStackTrace();
		}
		return "redirect:/subject";
	}
	@RequestMapping(value="/subjectChangeLocale")
	public String changeLocale(HttpServletRequest request , HttpServletResponse response , String localeType){
		if("en_US".equals(localeType)){ 
			localeResolver.setLocale(request, response, Locale.US);
		}else if("zh_CN".equals(localeType)){
			localeResolver.setLocale(request, response, Locale.CHINA);
		}
		return "redirect:/subject";
	}
	@ResponseBody
	@RequestMapping(value="/changeStatus",method = RequestMethod.POST)
	public boolean changeStatus(int sub_id){
		try {
			Subject subject = subjectService.getSubjectById(sub_id);
			if(subject != null){
				if(subject.getStatus() == 0){
					subject.setStatus(1);
				}else{
					subject.setStatus(0);
				}
				subjectService.editSubject(subject);
				return true;
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return false;
	}
	@ResponseBody
	@RequestMapping(value="/getAllStudent")
    public Map<String , Object> getAllStudent(Integer sub_id){
		System.out.println("我进来过了");
		Map<String ,Object> resultMap = new HashMap<String , Object>();
		try {
			resultMap.put("allUser", userService.findAllUsers());//获取所有学生，包括没有关联的学生
			resultMap.put("selectedUser",subjectService.getAllUserBySubId(sub_id));//获取已关联的学生
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return resultMap;
	}
	@ResponseBody
	@RequestMapping(value="/saveSubjectStudents")
	public Map<String , Object> saveSubjectStudents(@RequestParam(name = "subjectStudents[]")String[] subjectStudents , Integer sub_id){
		Map<String , Object> resultMap = new HashMap<String ,Object>();
		resultMap.put("success", false);
		System.out.println("我已经进到了这里");
		List<String> list = new ArrayList<String>();
		for(String user_id : subjectStudents){
			list.add(user_id.trim());
			System.out.println("我已经进到了这里"+user_id+"    "+sub_id);
		}
		try {
			subjectService.saveSubjectStudent(list,sub_id);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		resultMap.put("success", true);
		return resultMap;
	}
	
	


}
